class Solution {
public:
    int numberOfWeakCharacters(vector<vector<int>>& properties) {
      
        sort(properties.begin(), properties.end(), [](const vector<int>& a, const vector<int>& b) {
            if (a[0] == b[0]) return a[1] > b[1]; 
            return a[0] < b[0]; 
        });

        int ans = 0;
        stack<int> st;

        
        for (auto& p : properties) {
            int defense = p[1];
            while (!st.empty() && defense > st.top()) {
                st.pop();
                ans++;
            }
            st.push(defense);
        }

        return ans;
    }
};

